home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / WINER.ZIP / BUFIN.BAS < prev    next >
BASIC Source File  |  1992-05-13  |  3KB  |  93 lines

  1. '********* BUFIN.BAS - fast LINE INPUT replacement
  2.  
  3. 'Copyright (c) 1992 Ethan Winer
  4.  
  5. DEFINT A-Z
  6. DECLARE FUNCTION BufIn$ (FileName$, Done)
  7.  
  8. LINE INPUT "Enter a file name: ", FileName$
  9.  
  10. Start! = TIMER
  11. DO
  12.    This$ = BufIn$(FileName$, Done)
  13.    IF Done THEN EXIT DO
  14. LOOP
  15. Done! = TIMER
  16. PRINT "Buffered input: "; Done! - Start!
  17.  
  18.  
  19. Start! = TIMER
  20. OPEN FileName$ FOR INPUT AS #1
  21. DO
  22.    LINE INPUT #1, This$
  23. LOOP UNTIL EOF(1)
  24. Done! = TIMER
  25. PRINT " BASIC's INPUT: "; Done! - Start!
  26. CLOSE
  27. END
  28.  
  29. FUNCTION BufIn$ (FileName$, Done) STATIC
  30.  
  31. IF NOT Reading THEN            'if the first time through
  32.   Reading = -1                 'show that we're now reading
  33.   Done = 0                     'clear Done just in case
  34.   CR = 0                       'no return found yet.
  35.   CR$ = CHR$(13)               'define for speed later
  36.  
  37.   FileNum = FREEFILE           'open the file
  38.   OPEN FileName$ FOR BINARY AS #FileNum
  39.   Remaining& = LOF(FileNum)    'byte count to be read
  40.  
  41.   BufSize = 4096               'bytes to read each pass
  42.   Buffer$ = SPACE$(BufSize)    'assume BufSize bytes
  43. END IF
  44.  
  45. '---- This is the main outer loop.
  46. DO WHILE Remaining&              'while more in the file
  47.  
  48.   IF CR = 0 THEN                 'if no Return was found
  49.     IF Remaining& < BufSize THEN 'read only what remains
  50.       BufSize = Remaining&       'resize the buffer
  51.       IF BufSize < 1 THEN EXIT DO'possible only if EOF 26
  52.       Buffer$ = SPACE$(BufSize)  'create the file buffer
  53.     END IF
  54.     GET #FileNum, , Buffer$      'read a block
  55.     BufPos = 1                   'start at the beginning
  56.   END IF                         '  of that block
  57.  
  58.   DO                                 'walk through buffer
  59.     CR = INSTR(BufPos, Buffer$, CR$) 'look for a Return
  60.     IF CR THEN                       'we found one
  61.       SaveCR = CR                    'save where
  62.       BufIn$ = MID$(Buffer$, BufPos, CR - BufPos)
  63.       BufPos = CR + 2                'skip inevitable LF
  64.       EXIT FUNCTION                  'all done for now
  65.     ELSE                             'back up in the file
  66.       '---- If we reached the end of the file and no 13
  67.       '     was found, return what remains in the string.
  68.       IF SEEK(FileNum) >= LOF(FileNum) THEN
  69.         Output$ = MID$(Buffer$, SaveCR + 2)
  70.         '---- Trap a trailing CHR$(26) EOF marker.
  71.         IF RIGHT$(Output$, 1) = CHR$(26) THEN
  72.           Output$ = LEFT$(Output$, LEN(Output$) - 1)
  73.         END IF
  74.         BufIn$ = Output$             'assign the function
  75.         Remaining& = BufSize         'set to fall out
  76.         EXIT DO                      'and exit now
  77.       END IF
  78.       Slop = BufSize - SaveCR - 1    'calc buffer excess
  79.       Remaining& = Remaining& + Slop 'calc file excess
  80.       SEEK #FileNum, SEEK(FileNum) - Slop  'seek to start
  81.     END IF
  82.  
  83.   LOOP WHILE CR                'while more in buffer
  84.   Remaining& = Remaining& - BufSize
  85.  
  86. LOOP
  87.  
  88. Reading = 0                    'we're not reading anymore
  89. Done = -1                      'show that we're all done
  90. CLOSE #FileNum                 'final cleanup
  91.  
  92. END FUNCTION
  93.